import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
import _isEqual from "lodash/isEqual";
import _isEmpty from "lodash/isEmpty";

var _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _class, _class2;

/**
 * DataDimensionTab - 安全组数据权限
 * @date: 2019-12-24

 */
import React from 'react';
import { Form, Tabs, Spin, Card } from 'antd';
import { Bind } from 'lodash-decorators';
import { connect } from 'dva';
import intl from 'utils/intl';
import formatterCollections from 'utils/intl/formatterCollections';
import { loadDimensionAsync } from "../../../../../../customize/dimensions";
import Company from "./Detail/Company";
import AuthDetail from "../../../../../../components/AuthorityManagement/AuthDetail";
import DimensionTab from "../../../../../../components/AuthorityManagement";
import styles from "./index.less?modules";
var TabPane = Tabs.TabPane;
var DataPermissionTab = (_dec = formatterCollections({
  code: ['ciam.authority', 'entity.company', 'entity.customer', 'entity.supplier']
}), _dec2 = Form.create({
  fieldNameProp: null
}), _dec3 = connect(function (_ref) {
  var accSecGrpAuthorityValueList = _ref.accSecGrpAuthorityValueList,
      accSecGrpAuthorityPurorg = _ref.accSecGrpAuthorityPurorg,
      accSecGrpAuthorityPuragent = _ref.accSecGrpAuthorityPuragent,
      accSecGrpAuthorityLovView = _ref.accSecGrpAuthorityLovView,
      accSecGrpAuthorityDataSource = _ref.accSecGrpAuthorityDataSource,
      accSecGrpAuthorityDataGroup = _ref.accSecGrpAuthorityDataGroup,
      accSecGrpAuthorityCompany = _ref.accSecGrpAuthorityCompany,
      loading = _ref.loading;
  return {
    accSecGrpAuthorityCompany: accSecGrpAuthorityCompany,
    accSecGrpAuthorityValueList: accSecGrpAuthorityValueList,
    fetchValueListLoading: loading.effects['accSecGrpAuthorityValueList/fetchAuthorityValueList'],
    accSecGrpAuthorityPurorg: accSecGrpAuthorityPurorg,
    fetchPurorgLoading: loading.effects['accSecGrpAuthorityPurorg/fetchAuthorityPurorg'],
    accSecGrpAuthorityPuragent: accSecGrpAuthorityPuragent,
    fetchPuragentLoading: loading.effects['accSecGrpAuthorityPuragent/fetchAuthorityPuragent'],
    accSecGrpAuthorityLovView: accSecGrpAuthorityLovView,
    fetchLovViewLoading: loading.effects['accSecGrpAuthorityLovView/fetchAuthorityLovView'],
    accSecGrpAuthorityDataSource: accSecGrpAuthorityDataSource,
    fetchDataSourceLoading: loading.effects['accSecGrpAuthorityDataSource/fetchAuthorityDataSource'],
    accSecGrpAuthorityDataGroup: accSecGrpAuthorityDataGroup,
    fetchDataGroupLoading: loading.effects['accSecGrpAuthorityDataGroup/fetchAuthorityDataGroup']
  };
}), _dec4 = Bind(), _dec5 = Bind(), _dec6 = Bind(), _dec(_class = _dec2(_class = _dec3(_class = (_class2 = /*#__PURE__*/function (_React$Component) {
  _inherits(DataPermissionTab, _React$Component);

  var _super = _createSuper(DataPermissionTab);

  function DataPermissionTab(props) {
    var _this;

    _classCallCheck(this, DataPermissionTab);

    _this = _super.call(this, props);
    _this.state = {
      defaultList: [],
      tabList: []
    };
    return _this;
  }

  _createClass(DataPermissionTab, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var allTabList = this.props.allTabList;
      this.renderDefaultTabs();
      var defaultList = ['PURORG', 'LOV', 'DATASOURCE', 'LOV_VIEW', 'PURAGENT', 'COMPANY', 'DATA_GROUP'];
      var arr = allTabList.filter(function (item) {
        return defaultList.includes(item.dimensionCode) && item.valueSourceType === 'LOCAL';
      }).map(function (item) {
        return item.dimensionCode;
      });
      this.setState({
        defaultList: arr
      });
      this.loadDimensions(allTabList);
    } // eslint-disable-next-line camelcase

  }, {
    key: "UNSAFE_componentWillReceiveProps",
    value: function UNSAFE_componentWillReceiveProps(nextProps) {
      var allTabList = this.props.allTabList;
      this.renderDefaultTabs();

      if (!_isEqual(nextProps.allTabList, allTabList) && !_isEmpty(nextProps.allTabList)) {
        var defaultList = ['PURORG', 'LOV', 'DATASOURCE', 'LOV_VIEW', 'PURAGENT', 'COMPANY', 'DATA_GROUP'];
        var arr = nextProps.allTabList.filter(function (item) {
          return defaultList.includes(item.dimensionCode) && item.valueSourceType === 'LOCAL';
        }).map(function (item) {
          return item.dimensionCode;
        });
        this.setState({
          defaultList: arr
        });
        this.loadDimensions(nextProps.allTabList);
      }
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      var dispatch = this.props.dispatch; // TODO: 为了 清除缓存 需要在这里将所有model还原到初始化

      dispatch({
        type: 'roleDataAuthorityManagement/updateState',
        payload: {
          authorList: []
        }
      });
      dispatch({
        type: 'accSecGrpAuthorityCompany/updateState',
        payload: {
          data: [],
          checkList: [],
          originList: [],
          expandedRowKeys: []
        }
      });
      dispatch({
        type: 'accSecGrpAuthorityPuragent/updateState',
        payload: {
          head: {},
          // 头部数据
          list: [],
          // 请求查询到的数据
          pagination: {},
          // 分页信息
          puragentDataSource: [],
          puragentPagination: {}
        }
      });
      dispatch({
        type: 'accSecGrpAuthorityPurorg/updateState',
        payload: {
          head: {},
          // 头部数据
          list: [],
          // 请求查询到的数据
          pagination: {},
          // 分页信息
          purorgDataSource: [],
          purorgPagination: {}
        }
      });
      dispatch({
        type: 'accSecGrpAuthorityDataSource/updateState',
        payload: {
          head: {},
          // 头部数据
          list: [],
          // 请求查询到的数据
          pagination: {},
          // 分页信息
          createDataSource: [],
          createPagination: {}
        }
      });
      dispatch({
        type: 'accSecGrpAuthorityDataGroup/updateState',
        payload: {
          head: {},
          // 头部数据
          list: [],
          // 请求查询到的数据
          pagination: {},
          // 分页信息
          createDataSource: [],
          createPagination: {}
        }
      });
      dispatch({
        type: 'accSecGrpAuthorityValueList/updateState',
        payload: {
          head: {},
          // 头部数据
          list: [],
          // 请求查询到的数据
          pagination: {},
          // 分页信息
          createDataSource: [],
          createPagination: {}
        }
      });
    }
    /**
     * 加载单独的组件, 失败返回 失败的页面
     * @param {string} code - 维度编码
     * @return {React.Component|null}
     */

  }, {
    key: "importDimension",
    value: function () {
      var _importDimension = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(code) {
        var loadDimension;
        return _regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                loadDimension = null;
                _context.prev = 1;
                _context.next = 4;
                return loadDimensionAsync(code);

              case 4:
                loadDimension = _context.sent;
                _context.next = 10;
                break;

              case 7:
                _context.prev = 7;
                _context.t0 = _context["catch"](1);
                loadDimension = null;

              case 10:
                return _context.abrupt("return", loadDimension);

              case 11:
              case "end":
                return _context.stop();
            }
          }
        }, _callee, null, [[1, 7]]);
      }));

      function importDimension(_x) {
        return _importDimension.apply(this, arguments);
      }

      return importDimension;
    }()
    /**
     * 加载所有的维度组件
     */

  }, {
    key: "importDimensions",
    value: function () {
      var _importDimensions = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
        var _this2 = this;

        var _len,
            dimensions,
            _key,
            _args2 = arguments;

        return _regeneratorRuntime.wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                for (_len = _args2.length, dimensions = new Array(_len), _key = 0; _key < _len; _key++) {
                  dimensions[_key] = _args2[_key];
                }

                return _context2.abrupt("return", Promise.all(dimensions.map(function (item) {
                  return _this2.importDimension(item.valueSource);
                })));

              case 2:
              case "end":
                return _context2.stop();
            }
          }
        }, _callee2);
      }));

      function importDimensions() {
        return _importDimensions.apply(this, arguments);
      }

      return importDimensions;
    }()
    /**
     * 将 卡片 加载成 layout
     */

  }, {
    key: "loadDimensions",
    value: function loadDimensions() {
      var _this3 = this;

      var layouts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
      var _this$props = this.props,
          roleId = _this$props.roleId,
          tenantId = _this$props.tenantId,
          secGrpId = _this$props.secGrpId;
      var layout = layouts;
      var arr = [];
      var dimensions = [];
      this.importDimensions.apply(this, _toConsumableArray(layout)).then(function (cmps) {
        dimensions = layout.map(function (item, index) {
          var cmp = cmps[index];

          if (item.valueSourceType === 'LOV') {
            return {
              code: item.valueSource,
              name: item.dimensionName,
              valueSource: item.valueSource,
              component: /*#__PURE__*/React.createElement(DimensionTab, {
                code: item.viewCode,
                name: item.dimensionName,
                authorityTypeCode: item.dimensionCode,
                queryParams: {
                  roleId: roleId,
                  secGrpId: secGrpId
                },
                tenantId: tenantId,
                isSecGrp: true,
                isAccount: true
              })
            };
          }

          if (cmp) {
            arr.push(item.valueSource);

            if (cmp.__esModule) {
              var _CpaasDimension = cmp.default;
              return {
                code: item.valueSource,
                name: item.dimensionName,
                component: /*#__PURE__*/React.createElement(_CpaasDimension, {
                  dimensionParams: item,
                  tenantId: tenantId,
                  queryParams: {
                    roleId: roleId
                  }
                })
              };
            }

            var CpaasDimension = cmp;
            return {
              code: item.valueSource,
              name: item.dimensionName,
              component: /*#__PURE__*/React.createElement(CpaasDimension, {
                dimensionParams: item,
                tenantId: tenantId,
                queryParams: {
                  roleId: roleId
                }
              })
            };
          }

          return {
            code: item.valueSource,
            name: item.dimensionName,
            component: /*#__PURE__*/React.createElement(Card, {
              loading: true
            })
          };
        });
      }).finally(function () {
        var defaultList = _this3.state.defaultList;

        _this3.setState({
          dimensions: dimensions,
          defaultList: defaultList.filter(function (item) {
            return !arr.includes(item);
          })
        });
      });
    }
  }, {
    key: "renderDimensions",
    value: function renderDimensions() {
      var _this$state = this.state,
          _this$state$dimension = _this$state.dimensions,
          dimensions = _this$state$dimension === void 0 ? [] : _this$state$dimension,
          defaultList = _this$state.defaultList,
          tabList = _this$state.tabList;
      return dimensions.map(function (item) {
        if (!defaultList.includes(item.code)) {
          return /*#__PURE__*/React.createElement(TabPane, {
            tab: item.name,
            key: item.name
          }, item.component);
        }

        return /*#__PURE__*/React.createElement(TabPane, {
          tab: item.name,
          key: item.name
        }, tabList.find(function (temp) {
          return temp.code === item.code;
        }).component);
      }).filter(function (item) {
        return !!item;
      });
    }
  }, {
    key: "renderDefaultTabs",
    value: function renderDefaultTabs() {
      var _this$props2 = this.props,
          _this$props2$dispatch = _this$props2.dispatch,
          dispatch = _this$props2$dispatch === void 0 ? function (e) {
        return e;
      } : _this$props2$dispatch,
          path = _this$props2.path,
          roleId = _this$props2.roleId,
          secGrpId = _this$props2.secGrpId,
          _this$props2$onShield = _this$props2.onShield,
          onShield = _this$props2$onShield === void 0 ? function (e) {
        return e;
      } : _this$props2$onShield;
      var organizationId = this.state.organizationId;
      var companyProps = {
        path: path,
        dispatch: dispatch,
        organizationId: organizationId,
        secGrpId: secGrpId,
        roleId: roleId // onShield,
        // shieldLoading,

      };
      var commonProps = {
        path: path,
        dispatch: dispatch,
        organizationId: organizationId,
        roleId: roleId,
        isSecGrp: true,
        secGrpId: secGrpId,
        onShield: onShield,
        isShow: false,
        isAccount: true
      }; // 采购组织

      var purorgProps = _objectSpread(_objectSpread({}, commonProps), {}, {
        code: 'purorg',
        name: intl.get('ciam.authority.view.message.tab.purorg').d('采购组织')
      });

      var puragentProps = _objectSpread(_objectSpread({}, commonProps), {}, {
        code: 'puragent',
        name: intl.get('ciam.authority.view.message.tab.puragent').d('采购员')
      });

      var valueListProps = _objectSpread(_objectSpread({}, commonProps), {}, {
        code: 'valueList',
        name: intl.get('ciam.authority.view.message.tab.valueList').d('值集')
      });

      var lovViewProps = _objectSpread(_objectSpread({}, commonProps), {}, {
        code: 'lovView',
        name: intl.get('ciam.authority.view.message.tab.lovView').d('值集视图')
      }); // 数据源


      var dataSourceProps = _objectSpread(_objectSpread({}, commonProps), {}, {
        code: 'dataSource',
        name: intl.get('ciam.authority.view.message.tab.dataSource').d('数据源')
      }); // 数据组


      var dataGroupProps = _objectSpread(_objectSpread({}, commonProps), {}, {
        code: 'dataGroup',
        name: intl.get('ciam.authority.view.message.tab.dataGroup').d('数据组')
      });

      var tabList = [{
        code: 'COMPANY',
        component: /*#__PURE__*/React.createElement(Company, companyProps)
      }, {
        code: 'PURORG',
        component: /*#__PURE__*/React.createElement(AuthDetail, purorgProps)
      }, {
        code: 'PURAGENT',
        component: /*#__PURE__*/React.createElement(AuthDetail, puragentProps)
      }, {
        code: 'LOV',
        component: /*#__PURE__*/React.createElement(AuthDetail, valueListProps)
      }, {
        code: 'LOV_VIEW',
        component: /*#__PURE__*/React.createElement(AuthDetail, lovViewProps)
      }, {
        code: 'DATASOURCE',
        component: /*#__PURE__*/React.createElement(AuthDetail, dataSourceProps)
      }, {
        code: 'DATA_GROUP',
        component: /*#__PURE__*/React.createElement(AuthDetail, dataGroupProps)
      }];
      this.setState({
        tabList: tabList
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props3 = this.props,
          allTabList = _this$props3.allTabList,
          _this$props3$queryDat = _this$props3.queryDataPermissionTabLoading,
          queryDataPermissionTabLoading = _this$props3$queryDat === void 0 ? false : _this$props3$queryDat;
      return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
        style: {
          minHeight: '100px'
        }
      }, /*#__PURE__*/React.createElement(Spin, {
        spinning: queryDataPermissionTabLoading
      }, !_isEmpty(allTabList) && /*#__PURE__*/React.createElement(Tabs, {
        defaultActiveKey: "company",
        animated: false,
        tabPosition: "left",
        className: styles['sub-accout-tabs'],
        loading: false
      }, this.renderDimensions()), _isEmpty(allTabList) && /*#__PURE__*/React.createElement("h3", {
        style: {
          color: 'gray',
          marginTop: '10%',
          marginBottom: '10%',
          textAlign: 'center'
        }
      }, intl.get('ciam.authority.model.authorityManagement.noAuthority').d('当前暂无可维护的数据权限')))));
    }
  }]);

  return DataPermissionTab;
}(React.Component), (_applyDecoratedDescriptor(_class2.prototype, "loadDimensions", [_dec4], Object.getOwnPropertyDescriptor(_class2.prototype, "loadDimensions"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "renderDimensions", [_dec5], Object.getOwnPropertyDescriptor(_class2.prototype, "renderDimensions"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "renderDefaultTabs", [_dec6], Object.getOwnPropertyDescriptor(_class2.prototype, "renderDefaultTabs"), _class2.prototype)), _class2)) || _class) || _class) || _class);
export { DataPermissionTab as default };